<?php
class ecConfiguration{

  protected static $charset = "UTF-8";

  static function getCharset()
  {
    return self::$charset;
  }

  /**
   * Guesses the project root directory.
   *
   * @return string The project root directory
   */
  static public function guessRootDir()
  {
    $r = new ReflectionClass('ProjectConfiguration');

    return realpath(dirname($r->getFileName()).'/..');
  }

  /**
   * Returns an ApplicationConfiguration configuration for a given application.
   *
   * @param string            $application    An application name
   * @param string            $environment    The environment name
   * @param Boolean           $debug          true to enable debug mode
   * @param string            $rootDir        The project root directory
   *
   * @return ApplicationConfiguration A ApplicationConfiguration instance
   */
  static public function getApplicationConfiguration($environment, $debug, $rootDir = null)
  {

    //set environement
    ecEnvironment::init($environment, $debug);

    //chargement des classes
    if(ecEnvironment::getEnvironment() == 'dev')
    {
      ecAutoload::make();
    }

    #chargement des routes
    try{

      $route = new ecRouter();

      #Chargement des routes configuré dans routing.yml
      self::makeRouting($route);

      #Chargement des routes universelles par default
      $route->defaultRoute();

      $route->execute();
    }
    catch(Exception $e)
    {
      throw new Exception("Unable to load Routing ".$e->getMessage());
    }

    #Récupération de l'application par default
    $application = $route->getApplicationRoute('default');

    $class = $application.'Configuration';

    if (null === $rootDir)
    {
      $rootDir = self::guessRootDir();
    }

    if (!file_exists($file = $rootDir.'/apps/'.$application.'/settings/'.$class.'.class.php'))
    {
      throw new InvalidArgumentException(sprintf('The application "%s" does not exist.', $application));
    }

    try{

      require_once $file;

      #Chargement de l'application
      $applicationClass =  new $class($environment, $debug, $rootDir);

      #chargement de l'action
      ecController::getController( $route->getArrayRouteController(), $applicationClass->getControllers() );
    }
    catch(Exception $e)
    {
      throw new Exception("Unable to load application: ".$application." ".$e->getMessage());
    }

    return true;
  }

  public static function makeRouting($routeObject)
  {
    try{
      global $routes;

      $yaml = new sfYamlParser();

      if (($routes = apc_fetch('routes')) === false)
      {
        $routes = $yaml->parse(file_get_contents(dirname(__FILE__).'/../../settings/routing.yml'));
        apc_store('routes', serialize($routes), APC_TIMEOUT);
      }
      else
      {
        $routes = unserialize($routes);
      }

      //$routes = $yaml->parse(file_get_contents(dirname(__FILE__).'/../../settings/routing.yml'));

      foreach($routes as $route)
      {
        if(array_key_exists('condition', $route))
        {
          $routeObject->map($route['url'], $route['param'], $route['condition']);
        }
        else{
          $routeObject->map($route['url'], $route['param']);
        }
      }
    }
    catch (InvalidArgumentException $e)
    {
      // an error occurred during parsing
      throw new Exception("Unable to parse the YAML string: ".$e->getMessage());
    }
  }
}

?>
